
package com.showcai.controller.certificate;

import com.showcai.controller.base.BaseController;
import com.showcai.module.Certificate;
import com.showcai.module.User;
import com.showcai.myRunTime.MyRunTimeExcption;
import com.showcai.result.ResultObj;
import com.showcai.result.ReturnCode;
import com.showcai.service.CertificateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by lutao on 2017/7/14.
 */
@RequestMapping("/certificate")
@Controller
public class CertificateController extends BaseController {
 @Autowired
    CertificateService certificateService;

    /**
     * 获得卡券
     *
     * @return
     */
    @RequestMapping(value = "/userGain/{memberId}/{productEntityId}", method = RequestMethod.POST)
    @ResponseBody
    public String userGain(@PathVariable String memberId,@PathVariable Long productEntityId) {
        ResultObj resultObj = new ResultObj();
        User user=userService.getUserByThirdId(memberId);
        if(user==null){
            throw new MyRunTimeExcption(ReturnCode.USER_IS_INEXISTENCE);
        }
        Certificate certificate=certificateService.userGainCertificate(user.getTid(),productEntityId);
        resultObj.setData(certificate);
        resultObj.setMsg("操作成功！");
        return resultObj.toJson();
    }
    /**
     * 获取卡券详情
     *
     * @return
     */
    @RequestMapping(value = "/certificate/{certificateId}", method = RequestMethod.POST)
    @ResponseBody
    public String index(@PathVariable String certificateId) {
        ResultObj resultObj = new ResultObj();
        Certificate certificate=certificateService.getCertificateByCertificateId(certificateId);
        resultObj.setData(certificate);
        resultObj.setMsg("操作成功！");
        return resultObj.toJson();
    }


}
